package com.zoro.barn.inner.service.pp.project.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.zoro.barn.api.pp.project.BarnProjectService;
import com.zoro.barn.commons.tools.json.JsonUtil;
import com.zoro.barn.dto.pp.project.BarnProjectInfoDto;
import com.zoro.barn.dto.pp.project.projectinfodto.GetProjectInfoInfosReq;
import com.zoro.barn.inner.service.pp.project.entity.ProjectEntity;
import com.zoro.barn.inner.service.pp.project.service.ProjectService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhaoxingwu
 */
@RestController
@RequestMapping("project-info")
public class ProjectController implements BarnProjectService {

    private final ProjectService projectService;

    public ProjectController(ProjectService projectService) {
        this.projectService = projectService;
    }

    @Override
    public Map<String, BarnProjectInfoDto> getProjectInfo(GetProjectInfoInfosReq req) throws JsonProcessingException {
        if (req.getIds() == null || req.getIds().length == 0) {
            return null;
        }
        List<ProjectEntity> projectEntities = projectService.getByIds(req.getIds());
        if (CollectionUtils.isEmpty(projectEntities)) {
            return null;
        }
        List<BarnProjectInfoDto> result = JsonUtil.transformType(projectEntities, BarnProjectInfoDto.class);

        return result.stream().collect(Collectors.toMap(BarnProjectInfoDto::getPid, v -> v, (a, b) -> b));
    }
}
